package com.example.asus.homework11.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.asus.homework11.R;
import com.example.asus.homework11.bean.RegisterBean;
import com.example.asus.homework11.listener.RetrofitListener;
import com.example.asus.homework11.model.RegisterModel;


public class RegisterActivity extends AppCompatActivity implements RetrofitListener<RegisterBean>{
    private TextView et_username,et_password,et_password2,et_phone,et_address,et_comment;
    private Button bt_register,bt_return;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initViews();
        initEvent();
    }

    private void initViews() {
        et_username = (EditText)findViewById(R.id.et_username);
        et_password = (EditText)findViewById(R.id.et_password);
        et_password2 = (EditText)findViewById(R.id.et_password2);
        et_address = (EditText)findViewById(R.id.et_address);
        et_comment = (EditText)findViewById(R.id.et_comment);
        et_phone = (EditText)findViewById(R.id.et_checkNum);
        bt_register = (Button) findViewById(R.id.bt_register);

        bt_return = (Button)findViewById(R.id.bt_return);
    }
    private void initEvent(){
        bt_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (et_password.getText().toString().equals(et_password2.getText().toString()))
                   {
                        register();
                    }
                    else{
                    Toast.makeText(RegisterActivity.this, "密码与确认密码不一致", Toast.LENGTH_SHORT).show();
                }
                }

        });
        bt_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("user", et_username.toString());
                setResult(RESULT_OK,intent);
                finish();

            }
        });
    }
    private void register(){
        RegisterModel registerModel = new RegisterModel();
        registerModel.userRegister(et_username.getText().toString(),et_password.getText().toString(),
                et_address.getText().toString(),et_comment.getText().toString(),
                et_phone.getText().toString(),this);
    }
    @Override
    public void onSuccess(RegisterBean success, int flag) {
        if(success.getSuccess().equals("1")){
            Intent intent = new Intent();
            intent.putExtra("user",et_username.getText().toString());
            setResult(RESULT_OK,intent);
            finish();
            Toast.makeText(RegisterActivity.this, "注册成功 ", Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(RegisterActivity.this, "注册失败 ", Toast.LENGTH_SHORT).show();
        }
//        if(success.getSuccess().equals("0")){
//            Toast.makeText(RegisterActivity.this, "已经存在此用户", Toast.LENGTH_SHORT).show();
//        }
//        if(success.getSuccess().equals("1")) {
//          Intent intent = new Intent();
//          intent.putExtra("user",et_username.toString());
//          setResult(RESULT_OK,intent);
//          finish();;
//            Toast.makeText(RegisterActivity.this, "注册成功 ", Toast.LENGTH_SHORT).show();
//        }
//        else {
//            Toast.makeText(RegisterActivity.this, "注册失败 ", Toast.LENGTH_SHORT).show();
//        }
    }

    @Override
    public void onFail()  {
        Toast.makeText(RegisterActivity.this, "网络出错了", Toast.LENGTH_SHORT).show();

    }

}
